%author: wxj233
%time: 2023.10.25 12:00
%function: 主要用于完成特征点仿真以及轨迹模拟，整个场景在二维平面上进行

clc;
clear;
clear Function;
close all;


% 初始化仿真器
% dt
simulater = Simulater(1);

% 产生第一个目标特征点群
% len, width, probabilitys, varargin{seed}
cluster1 = simulater.generateFeaturePoints(10, 2.2, 1, 1);
% 根据特征点群，产生第一条轨迹
% cluster, point0, v, a, startTime, endTime, id, sdr, sdvr, sdtheta, varargin[seed1, seed2]
T = simulater.generateTrack_ra(cluster1, [10, 0], [0, 1], [0.01, -0.005], 0, 100, 1, 0.5, 0.15, 0.0001/3, 10, 20);  % [[t, r, theta, vr, x, y, vx, vy, id]; ...]
save('T.mat', 'T');

% % 产生第二个目标特征点群
% % len, width, probabilitys, varargin{seed}
% cluster2 = simulater.generateFeaturePoints(5, 2.2, [0.9, 0.8, 0.9], 3);
% % 根据特征点群，产生第二条轨迹
% % cluster, point0, v, a, startTime, endTime, id, sd, varargin
% T2 = simulater.generateTrack_xy(cluster2, [90, 0], [0, 1], [-0.01, -0.005], 0, 100, 2, 0.1, 0.1, 0.1, 10, 20);  % [t, x, y, id; ...]

% 引入噪声, 根据区域大小和噪声密度来算噪声数量，且数量服从泊松分布, 在区域内服从均匀分布，区域自己根据轨迹范围设定
% density, area
% noise = simulater.generateNoise(0.001, [0, 100, -20, 120], 1); % [t, x, y, 0; ...]

% vMax, dr, dvr, dtheta, q, p, vAssDoor, inDoor, fplifeMax, tlifeMax
mfpTracer = MFPTracer(10, 0.5*0.5, 0.15*0.15, 0.0001*0.0001/9, 0.01, 1, 0.95, 0.95, 20, 3);
[frame, isNextFrame]= simulater.getNextFrame();
frames = [];
while isNextFrame
    frame = mfpTracer.preproccess(frame, 20, 2, 1, 1, 10);  % 预处理points, epsilon, minpts, zoomX, zoomY, zoomV
    frames = [frames; frame];  % [1t, 2r, 3theta, 4vr, 5x, 6y, 7vx, 8vy, 9id, 10X, 11Y, 12cluster; ...]
    figure(1);
    gscatter(frames(:,10), frames(:,11), frames(:,12));hold on;  
    disp("当前第："+num2str(simulater.timeIndex)+"帧");  % 还未被追踪
    for track = mfpTracer.Tracks
        for fp = track.fps
            plot(fp.points(:,13), fp.points(:,15));hold on;
            te = "T"+num2str(fp.track.id)+" f:"+num2str(fp.id);
            text(fp.points(end,13), fp.points(end,15), te);
        end
    end
    hold off;
    axis([0 100 -20 120]);
    title("轨迹信息"+num2str(simulater.timeIndex));
    xlabel("x(m)");
    ylabel("y(m)");
    legend([]);
    grid on;
    if simulater.timeIndex == 85
        disp("pause");
    end
    
    mfpTracer.tracer(frame);
    [frame, isNextFrame]= simulater.getNextFrame();
end


